import { zwlogin } from '@/utils/http'
import aplus from '@/common/aplus'
import store from '@/utils/store'
import common from '@/utils/common'
import { zwapploginurl,alipayloginurl,zwappid, appname } from '@/common/config'
const login = {
	//验证是否登录
	isLogin(){
		// nickname
		if(!store.state.user_info||store.state.user_info==null||!store.state.user_info.nickname){
			const page = common.getPageUrl();
			console.log('isLogin_page',page)
			const app_type = uni.getStorageSync('app_type');
			if(app_type == 'zwapp'){
				location.href = zwapploginurl+'&goto='+encodeURIComponent(page)
			} else if(app_type == 'alipay'){
				location.href = alipayloginurl+'&goto='+encodeURIComponent(page);
			}
		}
	},
	//政务登陆
	zwlogin(ticket,url) {
		zwlogin({ ticket: ticket}).then(res => {
			if(res){
				console.log(res);
				uni.setStorageSync('user_info', res)
				store.commit('fn_set_user_info', res)
				let userType = null
				let long = null;
				let lati = null
				//应用开发管理平台-应用 ID
				const appid = zwappid;
				ZWJSBridge.getUserType()
					.then((result) => {
						userType = result.userType;
					}).catch((error) => {
						console.log(error);
					});
				ZWJSBridge.getLocation()
					.then((result) => {
						if(result.success){
							long = result.data.longitude
							lati = result.data.latitude
							// console.log(result)
							aplus.block()
							aplus.setinfomat(userType,appid,appname,long,lati);
						}
					}).catch((error) => {
						console.log(error);
					});
				//单点登录成功后获取
				aplus.setUsermat(res.openname, res.zwuserid);
				aplus.start()
				console.log(url);
				if(typeof(url) != "undefined" || url != ''){
					if(['/','/pages/index/index'].indexOf(url) < 0){
						uni.navigateTo({
							url
						});
					}
				}
				return ;
			}
		})
	},

	wxmpregister() {
		// await getProvider();
		let promise = '';
		promise = new Promise((resolve, reject) => {
			uni.getProvider({
				service: 'oauth',
				success: function (res) {
					console.log('first' + JSON.stringify(res))
					if (res.provider.indexOf('weixin') >= 0) {
						resolve(res)
					} else {
						reject(res);
					}
				}
			})
		}).then((res) => {
			//console.log(res)
			return new Promise((resolve, reject) => {
				uni.login({
					provider: 'weixin',
					scopes: 'auth_user',
					success: function (loginRes) {
						console.log('secoend' + JSON.stringify(loginRes))
						resolve(loginRes);
					}
				})
			})
		}).then((loginRes) => {
			console.log('three');
			return new Promise((resolve, reject) => {
				uni.getUserInfo({
					provider: 'weixin',
					success: function (res) {
						console.log('four');
						resolve({ iv: res.iv, encryptedData: res.encryptedData, signature: res.signature, code: loginRes.code })
					}
				})
			})
		})
		return promise;

	},
	wxlogin(success) {
		uni.login({
			provider: 'weixin',
			scopes: 'auth_user',
			success: function (res) {
				// console.log(res)
				let js_code = res.code;
				// console.log(res.code)
				uni.getUserInfo({
					provider: 'weixin',
					success: function (res) {
						console.log(res)
						typeof success == "function" && success(res);
						//that.register(res.iv,res.encryptedData,res.signature);
						// that.userInfo = res.userInfo
						// console.log(that.userInfo)
					}
				})
			}
		})
	},
}
export default login;